package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.CustLostMapper;
import com.woniuxy.dao.CustomerMapper;
import com.woniuxy.pojo.CustLost;
import com.woniuxy.pojo.Customer;
import com.woniuxy.pojo.Order;
import com.woniuxy.service.CustomerService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.vo.CustLostVo;
import com.woniuxy.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
   private CustLostMapper custLostMapper;
    @Override
    public DataGridView selectBycondition(CustLostVo custLostVo) {
        Page<Object> page=PageHelper.startPage(custLostVo.getPage(),custLostVo.getLimit());
        List<CustLost> custLosts=custLostMapper.selectBycondition(custLostVo);
        return  new DataGridView(page.getTotal(),custLosts);
    }
    @Autowired
    CustomerMapper customerMapper;
    @Override
    public DataGridView selectAll(CustLostVo custLostVo) {
        Page<Object> page=PageHelper.startPage(custLostVo.getPage(),custLostVo.getLimit());
        List<CustLost> custLosts=custLostMapper.selectAllCustLost();
        return  new DataGridView(page.getTotal(),custLosts);
    }
    //暂缓流失客户的状态和暂缓措施更改
    @Transactional
    @Override
    public void updateBycondition(CustLost custLost) {
       String delay= custLostMapper.selectDelay(custLost);
       custLost.setLostDelay(delay+"\t"+custLost.getLostDelay());
       custLostMapper.updateBycondition(custLost);

    }
    //确认流失客户的状态和原因更改,同时添加当前时间为确认流失时间@
    @Transactional
    @Override
    public void updateConfirmStatus(CustLost custLost) {
       //先查出原来的流失原因 并添加进去
        String reson=custLostMapper.selectReson(custLost);
        custLost.setLostReson(reson+"\t"+custLost.getLostReson());
        //添加时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=sdf.format(new Date());
        custLost.setLostLastDate(time);
        custLostMapper.updateConfirmStatus(custLost);
    }
    @Override
    public Customer selectByid(String id) {
      return customerMapper.selectByid(id);
    }
    @Override
    public List<Customer> selectAllCustomer() {
        return customerMapper.selectAllCustomer();
    }

    //查所有用户
    @Override
    public DataGridView selectAllCustomers(CustomerVo customerVo) {
        Page<Object> page=PageHelper.startPage(customerVo.getPage(),customerVo.getLimit());
        List<Customer> customers = this.customerMapper.selectCustomersByConditon(customerVo);
        return new DataGridView(page.getTotal(),customers);
    }
    //模糊条件查询用户
    /*@Override
    public List<Customer> selectCustomersByConditon(Customer customer) {
        return customerMapper.selectCustomersByConditon(customer);
    }*/
    //增加用户
    @Transactional
    @Override
    public boolean addCustomer(Customer customer) {
        boolean flag=false;
        int count=this.customerMapper.saveCustomer(customer);
        if(count>0){
            flag=true;
        }
        return flag;
    }
    //通过编号查顾客
    @Override
    public Customer selectCustomerByCustNo(String custNo) {
        return customerMapper.selectCustomerByCustNo(custNo);
    }
    //删除顾客
    @Transactional
    @Override
    public void deleteCustomerByCustNo(String custNo) {
        this.customerMapper.deleteCustomerByCustNo(custNo);

    }
    //删除
    @Transactional
    @Override
    public void deleteLost(CustLost custLost) {
            //先删除
            custLostMapper.deleteLost(custLost);
            //再把客户表中的状态改为0
        String custNo=custLost.getLostCustNo();
            customerMapper.updateCustFlag(custNo);
    }

    //批量删除
    @Transactional
    @Override
    public void removeBatchCustomer(String[] custNos) {
        if(custNos.length>0){
            for(String custNo:custNos){
                this.deleteCustomerByCustNo(custNo);
            }
        }

    }
    //更新顾客信息
    @Override
    public void updateCustomer(Customer customer) {
        this.customerMapper.updateCustomer(customer);
    }
    //改变顾客的flag
    @Override
    public void updateCustFlag(String orderCustNo) {
            customerMapper.updateCustFlag(orderCustNo);
    }
//从预警中新增到流失表
    @Override
    public void insertLost(String custNo) {
        //生成随机六位数字作为ID
        int lostStatus=2;
        int i=(int)((Math.random()*9+1)*100000);
            custLostMapper.insertLost(i,custNo,lostStatus);
    }

    @Override
    public List<Customer> selectAllWillLostCustomer1() {
        List<Customer> customerlist= customerMapper.selectAllCustomers();
        //存放符合条件的客户集合
        List<Customer> customers= new ArrayList<Customer>();
        for(Customer customer :customerlist){
            //查询到有订单
            Order order=customer.getLatelyOrder();
            if(order.getOrdeNo()!=0){
                //获取订单时间
                String time=order.getOrdeDate();
                try {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date1=sdf.parse(time);
                    Date date2=new Date();
                    //六个月为半年 182天
                    long i=(date2.getTime()-date1.getTime())/(1000*60*60*24);
                    if(i>182){
                        // System.out.println("流失了");
                        customers.add(customer);
                    }
                } catch (ParseException e) {
                    System.out.println("时间转换异常");
                }
            }else{
                customers.add(customer);
                //System.out.println("这里是没有订单，直接加到流失客户里面的");
            }
        }
        return customers;

    }

    //查询所有符合流失客户条件的客户
    @Override
    public DataGridView selectAllWillLostCustomer(CustomerVo customerVo) {
        int zz=0;
        Page<Object> page= PageHelper.startPage(customerVo.getPage(),customerVo.getLimit());
       List<Customer> customerlist= customerMapper.selectByCondition(customerVo);
       //存放符合条件的客户集合
       List<Customer> customers= new ArrayList<Customer>();
       for(Customer customer :customerlist){
           //查询到有订单
           Order order=customer.getLatelyOrder();
          if(order.getOrdeNo()!=0){
                //获取订单时间
                String time=order.getOrdeDate();
              try {
                  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  Date date1=sdf.parse(time);
                  Date date2=new Date();
                  //六个月为半年 182天
                  long i=(date2.getTime()-date1.getTime())/(1000*60*60*24);
                  if(i>182){
                     System.out.println(customer.getCustName()+"流失了"+"\n");
                      customers.add(customer);
                  }else{
                      zz++;
                  }
              } catch (ParseException e) {
                  System.out.println("时间转换异常");
              }
          }else{
              System.out.println(customer.getCustName()+"没有订单"+"\n");
              customers.add(customer);
              //System.out.println("这里是没有订单，直接加到流失客户里面的");
          }
       }
        System.out.println(zz);
        return new DataGridView(page.getTotal()-zz,customers);
    }


}
